﻿using BrightIdeasSoftware;
using ComponentFactory.Krypton.Toolkit;
using StableDiffusionTools.Imaging;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace VAELinker.Renderers
{
    public class ColoredTextRenderer : HighlightTextRenderer
    {
        internal const TextFormatFlags NormalTextFormatFlags = TextFormatFlags.NoPrefix |
                                                       TextFormatFlags.EndEllipsis |
                                                       TextFormatFlags.PreserveGraphicsTranslateTransform;

        private Dictionary<string, Color> colorDict = new Dictionary<string, Color>();
        private List<Color> colors = null;
        private Random r = new Random(6);

        private bool ColumnIsPrimary
        {
            get { return this.Column != null && this.Column.Index == 0; }
        }

        public ColoredTextRenderer() : base()
        {
            colors = new List<Color>();//new Color[]
            //{
                //Color.DarkBlue,
                //Color.DarkCyan,
                //Color.DarkGoldenrod,
                //Color.DarkGreen,
                //Color.DarkMagenta,
                //Color.DarkOliveGreen,
                //Color.DarkRed,
                //Color.DarkSlateBlue,
                //Color.DarkSlateGray,
                //Color.DarkViolet,
                //Color.Green,
                //Color.Indigo,
                //Color.MidnightBlue,
                //Color.Olive,
                //Color.Purple,
                //Color.SaddleBrown,
                //Color.SeaGreen,
                //Color.Sienna,
            //});

            int LSteps = 2;
            double LStart = 0.25;
            double LIncrement = 0.1;
            int HSteps = 12;
            double HIncrement = 1.0 / HSteps;
            for (int i = 0; i < LSteps; i++)
            {
                for (int j = 0; j < HSteps; j++)
                {
                    ColorHSL hSL = new ColorHSL(j * HIncrement, 1.0, LStart + (i * LIncrement), 1.0);
                    colors.Add(hSL.ToColor());
                    System.Diagnostics.Debug.WriteLine($"H{hSL.H}, L{hSL.L}");
                }
            }


            //foreach (string colorName in Enum.GetNames(typeof(KnownColor)))
            //{
            //    //cast the colorName into a KnownColor
            //    KnownColor knownColor = (KnownColor)Enum.Parse(typeof(KnownColor), colorName);
            //    //check if the knownColor variable is a System color
            //    if (knownColor > KnownColor.Transparent)
            //    {
            //        float l = Color.FromKnownColor(knownColor).GetBrightness();
            //        if (l < 0.45f)
            //            System.Diagnostics.Debug.WriteLine("Color."+colorName+",");
            //    }
            //}
        }

        private Color GetColor()
        {
            if (colors.Count > 0)
            {
                int index = r.Next(0, colors.Count);
                Color result = colors[index];
                colors.RemoveAt(index);
                return result;
            }
            else
            {
                return Color.Black;
            }
        }

        private Color GetColorForText(string text)
        {
            if (colorDict.TryGetValue(text, out Color color))
                return color;

            return colorDict[text] = GetColor();
        }

        /// <summary>
        /// Draw text using GDI
        /// </summary>
        /// <param name="g"></param>
        /// <param name="r"></param>
        /// <param name="txt"></param>
        protected override void DrawTextGdi(Graphics g, Rectangle r, string txt)
        {
            Color backColor = Color.Transparent;
            if (this.IsDrawBackground && this.IsItemSelected && ColumnIsPrimary && !this.ListView.FullRowSelect)
                backColor = this.GetSelectedBackgroundColor();

            TextFormatFlags flags = NormalTextFormatFlags | this.CellVerticalAlignmentAsTextFormatFlag;

            if (!this.CanWrap)
                flags |= TextFormatFlags.SingleLine;
            //var tag = (IHighlightItem)this.ListItem.RowObject;
            TextRenderer.DrawText(g, txt, this.Font, r, GetColorForText(txt), backColor, flags);
        }

        /// <summary>
        /// Draw the text using GDI+
        /// </summary>
        /// <param name="g"></param>
        /// <param name="r"></param>
        /// <param name="txt"></param>
        protected override void DrawTextGdiPlus(Graphics g, Rectangle r, string txt)
        {
            using (StringFormat fmt = this.StringFormatForGdiPlus)
            {
                Font f = this.Font;
                if (this.IsDrawBackground && this.IsItemSelected && this.ColumnIsPrimary && !this.ListView.FullRowSelect)
                {
                    SizeF size = g.MeasureString(txt, f, r.Width, fmt);
                    Rectangle r2 = r;
                    r2.Width = (int)size.Width + 1;
                    using (Brush brush = new SolidBrush(this.GetSelectedBackgroundColor()))
                    {
                        g.FillRectangle(brush, r2);
                    }
                }
                RectangleF rf = r;
                //rf.Width = 10000;
                using (Brush brush = new SolidBrush(GetColorForText(txt)))
                {
                    g.DrawString(txt, f, brush, rf, fmt);
                }
            }
        }
    }
}
